package com.cgycms.webdeploy.filter;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.util.StrUtil;
import com.cgycms.webdeploy.storage.DeployConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName : FileTypeFilter
 * @Description : 文件类型过滤
 * @Author : 王力超
 * @Date: 2020-11-05 15:17
 */
@Configuration
public class FileTypeFilter {

    @Autowired
    private DeployConfig deployConfig;

    public boolean valid(InputStream file) {
        try {

            String type = FileTypeUtil.getType(file);
            String fileType = deployConfig.getFileType();
            //未配置不限制file类型
            if (StrUtil.isBlank(fileType)) {
                return true;
            }
            List types = Arrays.asList(fileType.split(","));
            if (CollectionUtil.isEmpty(types)) {
                return true;
            }
            if (types.contains(type)) {
                return true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
